package com.asm.controller.api;

import com.asm.controller.BaseController;
import com.asm.dto.PageResult;
import com.asm.dto.Result;
import com.asm.service.UserService;
import com.asm.vo.sys.RoleAccessVo;
import com.asm.vo.sys.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author ZhuangGuiMan
 * @date 2019/07/13
 */
@RestController
@Api(value = "用户Controller")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    /**
     * 查询所有用户信息
     *
     * @return 所有用户信息
     */
    @ApiOperation("接口描述：获取所有用户信息")
    @GetMapping("/users")
    public ResponseEntity<Result> getAllUsers() {
        return new ResponseEntity(new Result().ok("success", userService.findAllUsers()), HttpStatus.OK);
    }

    /**
     * 根据角色代码查询用户信息
     *
     * @param roleCode 角色代码
     * @return 用户信息
     */
    @ApiOperation("接口描述：通过用户代码查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleCode", value = "角色代码")
    })
    @GetMapping("/users/{roleCode}")
    public ResponseEntity<Result> getUsersByRoleCode(@PathVariable("roleCode") String roleCode) {
        return new ResponseEntity(new Result().ok("success", userService.findUserByRoleCode(roleCode)), HttpStatus.OK);
    }

    /**
     * 添加用户
     *
     * @param userVo 用户
     * @return 操作状态
     */
    @ApiOperation("接口描述：添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userVo", value = "用户视图对象")
    })
    @PostMapping("/user/add")
    public ResponseEntity<Result> addUser(@RequestBody UserVo userVo) {
        Long id = userService.addUser(userVo);
        if (id == null) {
            return new ResponseEntity(new Result().ok("account exits"), HttpStatus.OK);
        }
        return new ResponseEntity(new Result().ok("add success"), HttpStatus.OK);
    }

    /**
     * 修改用户信息
     *
     * @param userVo 用户
     * @return 操作状态
     */
    @ApiOperation("接口描述：修改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userVo", value = "用户视图对象")
    })
    @PutMapping("/user/update")
    public ResponseEntity<Result> updateUser(@RequestBody UserVo userVo) {
        userService.updateUser(userVo);
        return new ResponseEntity<>(new Result().ok("update success"), HttpStatus.OK);
    }

    /**
     * 根据用户编号查询预定信息
     *
     * @param userId     用户编号
     * @param pageNumber 当前页码
     * @param pageSize   页面大小
     * @return 预定信息
     */
    @ApiOperation("接口描述：根据用户编号查询预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编号"),
            @ApiImplicitParam(name = "pageNumber", value = "页数"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")
    })
    @GetMapping("/reserveMsg/{userId}")
    public ResponseEntity<PageResult<Map<String, Object>>> getReserveMsgByUserId(@PathVariable("userId") Long userId, Integer pageNumber, Integer pageSize) {
        return new ResponseEntity(new Result().ok("success", userService.findReserveMsgByUserId(userId, pageNumber, pageSize)), HttpStatus.OK);
    }

    /**
     * 根据账号密码登录
     *
     * @param account  账号
     * @param password 密码
     * @return 用户id和角色代码
     */
    @ApiOperation("接口描述：根据账号密码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号"),
            @ApiImplicitParam(name = "password", value = "密码")
    })
    @GetMapping("/user/login")
    public ResponseEntity<Result> login(String account, String password) {
        RoleAccessVo roleAccessVo = userService.login(account, password);
        if (roleAccessVo == null) {
            return new ResponseEntity<>(new Result().ok("can't find this user"), HttpStatus.OK);
        }
        return new ResponseEntity<>(new Result().ok("successfully login", roleAccessVo), HttpStatus.OK);
    }

    /**
     * 根据账号和手机号码判断是否存在该用户
     *
     * @param account 账号
     * @param phone   手机号码
     * @return 响应信息
     */
    @ApiOperation("接口描述：根据账号和手机号码判断是否存在该用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号"),
            @ApiImplicitParam(name = "phone", value = "电话")
    })
    @GetMapping("/user/findUser")
    public ResponseEntity<Result> findUser(String account, String phone) {
        Map<String, Object> map = userService.findUser(account, phone);
        return new ResponseEntity<>(new Result().ok("success", map), HttpStatus.OK);
    }

    /**
     * 根据用户id获取用户手机号码
     *
     * @param userId 用户id
     * @return 手机号码以及响应信息
     */
    @ApiOperation("接口描述：根据用户id获取用户手机号码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编号")
    })
    @GetMapping("/user/getPhone")
    public ResponseEntity<Result> getPhoneByUserId(Long userId) {
        Map<String, String> map = userService.getPhoneByUserId(userId);
        return new ResponseEntity<>(new Result().ok("success", map), HttpStatus.OK);
    }

    /**
     * 传入用户id和新密码，然后修改用户的密码
     *
     * @param userId   用户id
     * @param password 新密码
     * @return 响应信息
     */
    @ApiOperation("接口描述：传入用户id和新密码，然后修改用户的密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id"),
            @ApiImplicitParam(name = "password", value = "用户密码")
    })
    @GetMapping("/user/changePassword")
    public ResponseEntity<Result> changePassword(Long userId, String password) {
        userService.changePassword(userId, password);
        return new ResponseEntity<>(new Result().ok("successfully change password"), HttpStatus.OK);
    }

    /**
     * 传入用户id和新手机号码，然后修改用户的手机号码
     *
     * @param userId 用户id
     * @param phone  新密码
     * @return 响应信息
     */
    @ApiOperation("接口描述：传入用户id和新手机号码，然后修改用户的手机号码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id"),
            @ApiImplicitParam(name = "phone", value = "电话")
    })
    @GetMapping("/user/changePhone")
    public ResponseEntity<Result> changePhone(Long userId, String phone) {
        userService.changePhone(userId, phone);
        return new ResponseEntity<>(new Result().ok("successfully change password"), HttpStatus.OK);
    }

    /**
     * 根据用户编号删除用户
     *
     * @param userId 用户编号
     * @return 操作状态
     */
    @ApiOperation("接口描述：根据用户编号删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编号")
    })
    @DeleteMapping("/user/delete/{userId}")
    public ResponseEntity<Result> deleteUserById(@PathVariable("userId") Long userId) {
        userService.deleteUserById(userId);
        return new ResponseEntity<>(new Result().ok("delete success"), HttpStatus.OK);
    }
}
